package ${serviceimplPackage};

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import ${mapperPackage}.${EntityName}Mapper;
import ${entityPackage}.${EntityName};
import ${voPackage}.${EntityName}Vo;
import ${servicePackage}.${EntityName}Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ${EntityName}ServiceImpl implements ${EntityName}Service {

    @Autowired
    ${EntityName}Mapper ${EntityName?uncap_first}Mapper;

    @Override
    public ${EntityName}Vo addOne(${EntityName}Vo ${EntityName?uncap_first}Vo) {
    ${EntityName} ${EntityName?uncap_first} = ${EntityName}.builder().build();
    BeanUtils.copyProperties(${EntityName?uncap_first}Vo, ${EntityName?uncap_first}, IgnoreStringUtil.ignoreExclude());
    int insert = ${EntityName?uncap_first}Mapper.insert(${EntityName?uncap_first});
    return BeanUtil.beanToVo(${EntityName?uncap_first}, ${EntityName?uncap_first}Vo);
    }

    @Override
    public ${EntityName}Vo getOneByCode(String code) {

    ${EntityName} ${EntityName?uncap_first} = this.${EntityName?uncap_first}Mapper.selectById(code);
    ${EntityName}Vo ${EntityName?uncap_first}Vo = BeanUtil.beanToVo(${EntityName?uncap_first}, new ${EntityName}Vo());

    return ${EntityName?uncap_first}Vo;
    }

    @Override
    public Integer update(${EntityName}Vo ${EntityName?uncap_first}Vo) {
    ${EntityName} ${EntityName?uncap_first} = BeanUtil.VoToBean(${EntityName?uncap_first}Vo,
    new ${EntityName}(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.${EntityName?uncap_first}Mapper.updateById(${EntityName?uncap_first});
    }

    @Override
    public Integer delete(String code) {
    return this.${EntityName?uncap_first}Mapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.${EntityName?uncap_first}Mapper.deleteBatchIds(codes);
    }

    @Override
    public List<${EntityName}Vo> findPage(Page page, ${EntityName}Vo ${EntityName?uncap_first}Vo) {
        this.${EntityName?uncap_first}Mapper.selectPage(page, this.getWrapper(${EntityName?uncap_first}Vo));
        List orders = page.getRecords();
        List<${EntityName}Vo> ${EntityName?uncap_first}Vos = new ArrayList<>();
        orders.forEach(e->{
            ${EntityName?uncap_first}Vos.add(BeanUtil.beanToVo(e, new ${EntityName}Vo()));
        });
        page.setRecords(null);
        return ${EntityName?uncap_first}Vos;
        }


    @Override
    public Integer count(${EntityName}Vo ${EntityName?uncap_first}Vo) {
        return this.${EntityName?uncap_first}Mapper.selectCount(this.getWrapper(${EntityName?uncap_first}Vo));
    }

    public AbstractWrapper getWrapper(${EntityName}Vo ${EntityName?uncap_first}Vo) {
        QueryWrapper<${EntityName}> wrapper = new QWrapper<>();
        if (${EntityName?uncap_first}Vo != null) {

            <#list table.likeProperties as like>
                if (StringUtils.isNotEmpty(${EntityName?uncap_first}Vo.get${like?cap_first}())) {
                    wrapper.like("${like}", ${EntityName?uncap_first}Vo.get${like?cap_first}());
                }
            </#list>

        }
        return wrapper;
    }
}
